package com.ys.peony.car300;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.ys.peony.R;
import com.ys.peony.model.domain.CarDetailModel;
import com.ys.peony.utils.PixelUtils;
import com.ys.peony.utils.YsUtil;
import com.ys.peony.view.inter.YsDailogClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 59153 on 2017/8/9.
 */

public class CarSelectDialog implements View.OnClickListener {
    Dialog mDialog;
    private Context mContext;
    private List<ItemModel> mItemModels;
    private Window mWindow;
    private YsDailogClickListener mYsDailogClickListener;
    private List mList;

    public CarSelectDialog(Context context, YsDailogClickListener listener) {
        mContext = context;
        mYsDailogClickListener = listener;
        init();
    }

    private void init() {
        mItemModels = new ArrayList<>();
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_dialog_car_select, null, false);
        mDialog = new Dialog(mContext, R.style.dialog);
        mDialog.setContentView(view);
        mDialog.setCanceledOnTouchOutside(false);
        mWindow = mDialog.getWindow();
        mWindow.setGravity(Gravity.CENTER);
        mWindow.setLayout(YsUtil.getScreenWidth(mContext) - PixelUtils.dp2px(50), mWindow.getAttributes().height);
        updateView(view);
    }


    private void updateView(View view) {
        CarDetailModel carDetailModel = CarDataManager.getInstance().getCarDetailModel();
        List<List<CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean>> list = carDetailModel.getBusiMortgageVo().getBusivehicleandslipvoList();
        CarDetailModel.BusiMortgageVoBean.BusivehicleandslipvoListBean busivehicleandslipvoListBean = list.get(list.size() - 2).get(0);
        mList = list;
        if (busivehicleandslipvoListBean != null) {
            ((TextView) view.findViewById(R.id.plate_number)).setText(busivehicleandslipvoListBean.getPlate_number());
            ((TextView) view.findViewById(R.id.car_brand)).setText(busivehicleandslipvoListBean.getBrand_name());
            ((TextView) view.findViewById(R.id.car_series)).setText(busivehicleandslipvoListBean.getSeries_id_desr());
        }
        busivehicleandslipvoListBean = list.get(list.size() - 1).get(0);
        if (busivehicleandslipvoListBean != null) {
            ((TextView) view.findViewById(R.id.plate_number2)).setText(busivehicleandslipvoListBean.getPlate_number());
            ((TextView) view.findViewById(R.id.car_brand2)).setText(busivehicleandslipvoListBean.getBrand_name());
            ((TextView) view.findViewById(R.id.car_series2)).setText(busivehicleandslipvoListBean.getSeries_id_desr());
        }
        view.findViewById(R.id.rl1).setOnClickListener(this);
        view.findViewById(R.id.rl2).setOnClickListener(this);
        view.findViewById(R.id.delete).setOnClickListener(this);
    }

    public void show() {
        if (mDialog != null) {
            mDialog.show();
        }
    }

    public void dismiss() {
        mDialog.dismiss();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl1:
                dismiss();
                if (mList != null && mList.size() >= 2) {
                    mYsDailogClickListener.clickDialogItemModel(mList.size() - 2, null);
                }
                break;
            case R.id.rl2:
                dismiss();
                if (mList != null && mList.size() >= 2) {
                    mYsDailogClickListener.clickDialogItemModel(mList.size() - 1, null);
                }
                break;
            case R.id.delete:
                dismiss();
                break;
        }
    }
}
